home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / MySql / sql-bench / test-insert.sh < prev    next >
Encoding:
Text File  |  2001-01-01  |  45.1 KB  |  1,614 lines

  1. #!@PERL@
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18. #
  19. # Test of creating a simple table and inserting $record_count records in it,
  20. # $opt_loop_count rows in order, $opt_loop_count rows in reverse order and
  21. # $opt_loop_count rows in random order
  22. #
  23. # changes made for Oracle compatibility
  24. # - $limits{'func_odbc_mod'} is OK from crash-me, but it fails here so set we
  25. #   set it to 0 in server-cfg
  26. # - the default server config runs out of rollback segments, so I added a couple
  27. #   of disconnect/connects to reset
  28. ##################### Standard benchmark inits ##############################
  29.  
  30. use DBI;
  31. use Benchmark;
  32.  
  33. $opt_loop_count=100000;        # number of rows/3
  34. $small_loop_count=10;        # Loop for full table retrieval
  35. $range_loop_count=$small_loop_count*50;
  36. $many_keys_loop_count=$opt_loop_count;
  37. $opt_read_key_loop_count=$opt_loop_count;
  38.  
  39. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  40. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  41.  
  42. if ($opt_loop_count < 256)
  43. {
  44.   $opt_loop_count=256;        # Some tests must have some data to work!
  45. }
  46.  
  47. if ($opt_small_test)
  48. {
  49.   $opt_loop_count/=100;
  50.   $many_keys_loop_count=$opt_loop_count/10;
  51.   $range_loop_count=10;
  52.   $opt_read_key_loop_count=10;
  53. }
  54. elsif ($opt_small_tables)
  55. {
  56.   $opt_loop_count=10000;        # number of rows/3
  57.   $many_keys_loop_count=$opt_loop_count;
  58.   $opt_read_key_loop_count=10;
  59. }
  60. elsif ($opt_small_key_tables)
  61. {
  62.   $many_keys_loop_count/=10;
  63. }
  64.  
  65. print "Testing the speed of inserting data into 1 table and do some selects on it.\n";
  66. print "The tests are done with a table that has $opt_loop_count rows.\n\n";
  67.  
  68. ####
  69. #### Generating random keys
  70. ####
  71.  
  72. print "Generating random keys\n";
  73. $random[$opt_loop_count]=0;
  74. for ($i=0 ; $i < $opt_loop_count ; $i++)
  75. {
  76.   $random[$i]=$i+$opt_loop_count;
  77. }
  78.  
  79. my $tmpvar=1;
  80. for ($i=0 ; $i < $opt_loop_count ; $i++)
  81. {
  82.   $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  83.   $swap=$tmpvar % $opt_loop_count;
  84.   $tmp=$random[$i]; $random[$i]=$random[$swap]; $random[$swap]=$tmp;
  85. }
  86.  
  87. $total_rows=$opt_loop_count*3;
  88.  
  89. ####
  90. ####  Connect and start timeing
  91. ####
  92. $start_time=new Benchmark;
  93. $dbh = $server->connect();
  94. ####
  95. #### Create needed tables
  96. ####
  97.  
  98. goto keys_test if ($opt_stage == 2);
  99. goto select_test if ($opt_skip_create);
  100.  
  101. print "Creating tables\n";
  102. $dbh->do("drop table bench1" . $server->{'drop_attr'});
  103. $dbh->do("drop table bench2" . $server->{'drop_attr'});
  104. $dbh->do("drop table bench3" . $server->{'drop_attr'});
  105. do_many($dbh,$server->create("bench1",
  106.                  ["id int NOT NULL",
  107.                   "id2 int NOT NULL",
  108.                   "id3 int NOT NULL",
  109.                   "dummy1 char(30)"],
  110.                  ["primary key (id,id2)",
  111.                  "index index_id3 (id3)"]));
  112.  
  113. if ($opt_lock_tables)
  114. {
  115.   $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  116. }
  117.  
  118. ####
  119. #### Insert $total_rows records in order, in reverse order and random.
  120. ####
  121.  
  122. $loop_time=new Benchmark;
  123.  
  124. if ($opt_fast_insert)
  125. {
  126.   $query="insert into bench1 values ";
  127. }
  128. else
  129. {
  130.   $query="insert into bench1 (id,id2,id3,dummy1) values ";
  131. }
  132.  
  133. if (($opt_fast || $opt_fast_insert) && $limits->{'insert_multi_value'})
  134. {
  135.   $query_size=$server->{'limits'}->{'query_size'};
  136.  
  137.   print "Inserting $opt_loop_count multiple-value rows in order\n";
  138.   $res=$query;
  139.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  140.   {
  141.     $tmp= "($i,$i,$i,'ABCDEFGHIJ'),";
  142.     if (length($tmp)+length($res) < $query_size)
  143.     {
  144.       $res.= $tmp;
  145.     }
  146.     else
  147.     {
  148.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  149.       $res=$query . $tmp;
  150.     }
  151.   }
  152.   print "Inserting $opt_loop_count multiple-value rows in reverse order\n";
  153.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  154.   {
  155.     $tmp= "(" . ($total_rows-1-$i) . "," .($total_rows-1-$i) .
  156.       "," .($total_rows-1-$i) . ",'BCDEFGHIJK'),";
  157.     if (length($tmp)+length($res) < $query_size)
  158.     {
  159.       $res.= $tmp;
  160.     }
  161.     else
  162.     {
  163.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  164.       $res=$query . $tmp;
  165.     }
  166.   }
  167.   print "Inserting $opt_loop_count multiple-value rows in random order\n";
  168.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  169.   {
  170.     $tmp= "(" . $random[$i] . "," . $random[$i] . "," . $random[$i] .
  171.       ",'CDEFGHIJKL')," or die $DBI::errstr;
  172.     if (length($tmp)+length($res) < $query_size)
  173.     {
  174.       $res.= $tmp;
  175.     }
  176.     else
  177.     {
  178.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  179.       $res=$query . $tmp;
  180.     }
  181.   }
  182.   $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  183. }
  184. else
  185. {
  186.   print "Inserting $opt_loop_count rows in order\n";
  187.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  188.   {
  189.     $sth = $dbh->do($query . "($i,$i,$i,'ABCDEFGHIJ')") or die $DBI::errstr;
  190.   }
  191.  
  192.   print "Inserting $opt_loop_count rows in reverse order\n";
  193.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  194.   {
  195.     $sth = $dbh->do($query . "(" . ($total_rows-1-$i) . "," .
  196.             ($total_rows-1-$i) . "," .
  197.             ($total_rows-1-$i) . ",'BCDEFGHIJK')")
  198.       or die $DBI::errstr;
  199.   }
  200.  
  201.   print "Inserting $opt_loop_count rows in random order\n";
  202.  
  203.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  204.   {
  205.     $sth = $dbh->do($query . "(". $random[$i] . "," . $random[$i] .
  206.             "," . $random[$i] . ",'CDEFGHIJKL')") or die $DBI::errstr;
  207.   }
  208. }
  209.  
  210. $end_time=new Benchmark;
  211. print "Time for insert (" . ($total_rows) . "): " .
  212.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  213.  
  214. if ($opt_lock_tables)
  215. {
  216.   $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  217. }
  218. if ($opt_fast && defined($server->{vacuum}))
  219. {
  220.   $server->vacuum(1,\$dbh,"bench1");
  221. }
  222. if ($opt_lock_tables)
  223. {
  224.   $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  225. }
  226.  
  227. ####
  228. #### insert $opt_loop_count records with duplicate id
  229. ####
  230.  
  231. if ($limits->{'unique_index'})
  232. {
  233.   print "Testing insert of duplicates\n";
  234.   $loop_time=new Benchmark;
  235.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  236.   {
  237.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  238.     $tmp=$tmpvar % ($total_rows);
  239.     $tmpquery = "$query ($tmp,$tmp,2,'D')";
  240.     if ($dbh->do($tmpquery))
  241.     {
  242.       die "Didn't get an error when inserting duplicate record $tmp\n";
  243.     }
  244.   }
  245.  
  246.   $end_time=new Benchmark;
  247.   print "Time for insert_duplicates (" . ($opt_loop_count) . "): " .
  248.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  249. }
  250.  
  251. #if ($opt_fast && defined($server->{vacuum}))
  252. #{
  253. #  $server->vacuum(1,\$dbh);
  254. #}
  255.  
  256. ####
  257. #### Do some selects on the table
  258. ####
  259.  
  260. select_test:
  261.  
  262. print "Retrieving data from the table\n";
  263. $loop_time=new Benchmark;
  264. $error=0;
  265.  
  266. # It's really a small table, so we can try a select on everything
  267.  
  268. $count=0;
  269. for ($i=1 ; $i <= $small_loop_count ; $i++)
  270. {
  271.   if (($found_rows=fetch_all_rows($dbh,"select id from bench1")) !=
  272.       $total_rows)
  273.   {
  274.     if (!$error++)
  275.     {
  276.       print "Warning: Got $found_rows rows when selecting a whole table of " . ($total_rows) . " rows\nContact the database or DBD author!\n";
  277.     }
  278.   }
  279.   $count+=$found_rows;
  280. }
  281.  
  282. $end_time=new Benchmark;
  283. print "Time for select_big ($small_loop_count:$count): " .
  284.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  285.  
  286. #
  287. # Do a lot of different ORDER BY queries
  288. #
  289.  
  290. $loop_time=new Benchmark;
  291. $estimated=$rows=0;
  292. for ($i=1 ; $i <= $small_loop_count ; $i++)
  293. {
  294.   $rows+=fetch_all_rows($dbh,"select id,id2 from bench1 order by id,id2",1);
  295.   $end_time=new Benchmark;
  296.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  297.                      $small_loop_count));
  298. }
  299. if ($estimated)
  300. { print "Estimated time"; }
  301. else
  302. { print "Time"; }
  303. print " for order_by_big_key ($small_loop_count:$rows): " .
  304.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  305.  
  306.  
  307. $loop_time=new Benchmark;
  308. $estimated=$rows=0;
  309. for ($i=1 ; $i <= $small_loop_count ; $i++)
  310. {
  311.   $rows+=fetch_all_rows($dbh,"select id,id2 from bench1 order by id desc, id2 desc",1);
  312.   $end_time=new Benchmark;
  313.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  314.                      $small_loop_count));
  315. }
  316. if ($estimated)
  317. { print "Estimated time"; }
  318. else
  319. { print "Time"; }
  320. print " for order_by_big_key_desc ($small_loop_count:$rows): " .
  321.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  322.  
  323.  
  324. $loop_time=new Benchmark;
  325. $estimated=$rows=0;
  326. for ($i=1 ; $i <= $small_loop_count ; $i++)
  327. {
  328.   $rows+=fetch_all_rows($dbh,"select id from bench1 order by id desc",1);
  329.   $end_time=new Benchmark;
  330.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  331.                      $small_loop_count));
  332. }
  333. if ($estimated)
  334. { print "Estimated time"; }
  335. else
  336. { print "Time"; }
  337. print " for order_by_big_key_prefix ($small_loop_count:$rows): " .
  338.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  339.  
  340.  
  341. $loop_time=new Benchmark;
  342. $estimated=$rows=0;
  343. for ($i=1 ; $i <= $small_loop_count ; $i++)
  344. {
  345.   $rows+=fetch_all_rows($dbh,"select id3 from bench1 order by id3",1);
  346.   $end_time=new Benchmark;
  347.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  348.                      $small_loop_count));
  349. }
  350. if ($estimated)
  351. { print "Estimated time"; }
  352. else
  353. { print "Time"; }
  354. print " for order_by_big_key2 ($small_loop_count:$rows): " .
  355.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  356.  
  357.  
  358. $sel=$limits->{'order_by_unused'} ? "id2" : "*";
  359. $loop_time=new Benchmark;
  360. $estimated=$rows=0;
  361. for ($i=1 ; $i <= $small_loop_count ; $i++)
  362. {
  363.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 order by id3",1);
  364.   $end_time=new Benchmark;
  365.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  366.                      $small_loop_count));
  367. }
  368. if ($estimated)
  369. { print "Estimated time"; }
  370. else
  371. { print "Time"; }
  372. print " for order_by_big_key_diff ($small_loop_count:$rows): " .
  373.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  374.  
  375.  
  376. $sel=$limits->{'order_by_unused'} ? "id" : "*";
  377. $loop_time=new Benchmark;
  378. $estimated=$rows=0;
  379. for ($i=1 ; $i <= $small_loop_count ; $i++)
  380. {
  381.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 order by id2,id3",1);
  382.   $end_time=new Benchmark;
  383.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  384.                      $small_loop_count));
  385. }
  386. if ($estimated)
  387. { print "Estimated time"; }
  388. else
  389. { print "Time"; }
  390. print " for order_by_big ($small_loop_count:$rows): " .
  391.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  392.  
  393.  
  394. $sel=$limits->{'order_by_unused'} ? "dummy1" : "dummy1,id3";
  395. $loop_time=new Benchmark;
  396. $estimated=$rows=0;
  397. for ($i=1 ; $i <= $range_loop_count ; $i++)
  398. {
  399.   $start=$opt_loop_count/$range_loop_count*$i;
  400.   $end=$start+$i;
  401.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 where id>=$start and id <= $end order by id3",1);
  402.   $end_time=new Benchmark;
  403.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  404.                      $range_loop_count));
  405. }
  406. if ($estimated)
  407. { print "Estimated time"; }
  408. else
  409. { print "Time"; }
  410. print " for order_by_range ($range_loop_count:$rows): " .
  411.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  412.  
  413. $sel=$limits->{'order_by_unused'} ? "dummy1" : "dummy1,id";
  414. $loop_time=new Benchmark;
  415. $estimated=$rows=0;
  416. for ($i=1 ; $i <= $range_loop_count ; $i++)
  417. {
  418.   $start=$opt_loop_count/$range_loop_count*$i;
  419.   $end=$start+$i;
  420.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 where id>=$start and id <= $end order by id",1);
  421.   $end_time=new Benchmark;
  422.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  423.                      $range_loop_count));
  424. }
  425. if ($estimated)
  426. { print "Estimated time"; }
  427. else
  428. { print "Time"; }
  429. print " for order_by_key_prefix ($range_loop_count:$rows): " .
  430.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  431.  
  432. $sel=$limits->{'order_by_unused'} ? "id2" : "id2,id3";
  433. $loop_time=new Benchmark;
  434. $estimated=$rows=0;
  435. for ($i=1 ; $i <= $range_loop_count ; $i++)
  436. {
  437.   $start=$opt_loop_count/$range_loop_count*$i;
  438.   $end=$start+$range_loop_count;
  439.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 where id3>=$start and id3 <= $end order by id3",1);
  440.   $end_time=new Benchmark;
  441.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  442.                      $range_loop_count));
  443. }
  444. if ($estimated)
  445. { print "Estimated time"; }
  446. else
  447. { print "Time"; }
  448. print " for order_by_key2_diff ($range_loop_count:$rows): " .
  449.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  450.  
  451. #
  452. # Test of select on 2 different keys with or
  453. # (In this case database can only use keys if they do an automatic union).
  454. #
  455.  
  456. $loop_time=new Benchmark;
  457. $estimated=0;
  458. $rows=0;
  459. $count=0;
  460. for ($i=1 ; $i <= $range_loop_count ; $i++)
  461. {
  462.   my $rnd=$i;
  463.   my $rnd2=$random[$i];
  464.   $rows+=fetch_all_rows($dbh,"select id2 from bench1 where id=$rnd or id3=$rnd2",1);
  465.   $count++;
  466.   $end_time=new Benchmark;
  467.   last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  468.                      $range_loop_count));
  469. }
  470. if ($estimated)
  471. { print "Estimated time"; }
  472. else
  473. { print "Time"; }
  474. print " for select_diff_key ($count:$rows): " .
  475.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  476.  
  477.  
  478. # Test select that is very popular when using ODBC
  479.  
  480. check_or_range("id","select_range_prefix");
  481. check_or_range("id3","select_range_key2");
  482.  
  483. # Check reading on direct key on id and id3
  484.  
  485. check_select_key("*","id","select_key_prefix");
  486. check_select_key2("*","id","id2","select_key");
  487. check_select_key2("id,id2","id","id2","select_key_return_key");
  488. check_select_key("*","id3","select_key2");
  489. check_select_key("id3","id3","select_key2_return_key");
  490. check_select_key("id,id2","id3","select_key2_return_prim");
  491.  
  492. ####
  493. #### A lot of simple selects on ranges
  494. ####
  495.  
  496. @Q=("select * from bench1 where !id!=3 or !id!=2 or !id!=1 or !id!=4 or !id!=16 or !id!=10",
  497.     6,
  498.     "select * from bench1 where !id!>=" . ($total_rows-1) ." or !id!<1",
  499.     2,
  500.     "select * from bench1 where !id!>=1 and !id!<=2",
  501.     2,
  502.     "select * from bench1 where (!id!>=1 and !id!<=2) or (!id!>=1 and !id!<=2)",
  503.     2,
  504.     "select * from bench1 where !id!>=1 and !id!<=10 and !id!<=5",
  505.     5,
  506.     "select * from bench1 where (!id!>0 and !id!<2) or !id!>=" . ($total_rows-1),
  507.     2,
  508.     "select * from bench1 where (!id!>0 and !id!<2) or (!id!>= " . ($opt_loop_count/2) . " and !id! <= " . ($opt_loop_count/2+2) . ") or !id! = " . ($opt_loop_count/2-1),
  509.     5,
  510.     "select * from bench1 where (!id!>=5 and !id!<=10) or (!id!>=1 and !id!<=4)",
  511.     10,
  512.     "select * from bench1 where (!id!=1 or !id!=2) and (!id!=3 or !id!=4)",
  513.     0,
  514.     "select * from bench1 where (!id!=1 or !id!=2) and (!id!=2 or !id!=3)",
  515.     1,
  516.     "select * from bench1 where (!id!=1 or !id!=5 or !id!=20 or !id!=40) and (!id!=1 or !id!>=20 or !id!=4)",
  517.     3,
  518.     "select * from bench1 where ((!id!=1 or !id!=3) or (!id!>1 and !id!<3)) and !id!<=2",
  519.     2,
  520.     "select * from bench1 where (!id! >= 0 and !id! < 4) or (!id! >=4 and !id! < 6)",
  521.     6,
  522.     "select * from bench1 where !id! <= -1 or (!id! >= 0 and !id! <= 5) or (!id! >=4 and !id! < 6) or (!id! >=6 and !id! <=7) or (!id!>7 and !id! <= 8)",
  523.     9,
  524.     "select * from bench1 where (!id!>=1 and !id!<=2 or !id!>=4 and !id!<=5) or (!id!>=0 and !id! <=10)",
  525.     11,
  526.     "select * from bench1 where (!id!>=1 and !id!<=2 or !id!>=4 and !id!<=5) or (!id!>2 and !id! <=10)",
  527.     10,
  528.     "select * from bench1 where (!id!>1 or !id! <1) and !id!<=2",
  529.     2,
  530.     "select * from bench1 where !id! <= 2 and (!id!>1 or !id! <=1)",
  531.     3,
  532.     "select * from bench1 where (!id!>=1 or !id! <1) and !id!<=2",
  533.     3,
  534.     "select * from bench1 where (!id!>=1 or !id! <=2) and !id!<=2",
  535.     3
  536.     );
  537.  
  538. print "\nTest of compares with simple ranges\n";
  539. check_select_range("id","select_range_prefix");
  540. check_select_range("id3","select_range_key2");
  541.  
  542. ####
  543. #### Some group queries
  544. ####
  545.  
  546. if ($limits->{'group_functions'})
  547. {
  548.   $loop_time=new Benchmark;
  549.   $count=1;
  550.  
  551.   $estimated=0;
  552.   for ($tests=0 ; $tests < $small_loop_count ; $tests++)
  553.   {
  554.     $sth=$dbh->prepare($query="select count(*) from bench1") or die $DBI::errstr;
  555.     $sth->execute or die $sth->errstr;
  556.     if (($sth->fetchrow_array)[0] != $total_rows)
  557.     {
  558.       print "Warning: '$query' returned wrong result\n";
  559.     }
  560.     $sth->finish;
  561.  
  562.     # min, max in keys are very normal
  563.     $count+=7;
  564.     fetch_all_rows($dbh,"select min(id) from bench1");
  565.     fetch_all_rows($dbh,"select max(id) from bench1");
  566.     fetch_all_rows($dbh,"select sum(id+0.0) from bench1");
  567.     fetch_all_rows($dbh,"select min(id3),max(id3),sum(id3 +0.0) from bench1");
  568.     if ($limits->{'group_func_sql_min_str'})
  569.     {
  570.       fetch_all_rows($dbh,"select min(dummy1),max(dummy1) from bench1");
  571.     }
  572.     $count++;
  573.     $sth=$dbh->prepare($query="select count(*) from bench1 where id >= " .
  574.                ($opt_loop_count*2)) or die $DBI::errstr;
  575.     $sth->execute or die $DBI::errstr;
  576.     if (($sth->fetchrow_array)[0] != $opt_loop_count)
  577.     {
  578.       print "Warning: '$query' returned wrong result\n";
  579.     }
  580.     $sth->finish;
  581.  
  582.  
  583.     $count++;
  584.     $sth=$dbh->prepare($query="select count(*),sum(id+0.0),min(id),max(id),avg(id+0.0) from bench1") or die $DBI::errstr;
  585.     $sth->execute or die $DBI::errstr;
  586.     @row=$sth->fetchrow_array;
  587.     if ($row[0] != $total_rows ||
  588.     int($row[1]+0.5) != int((($total_rows-1)/2*$total_rows)+0.5) ||
  589.     $row[2] != 0 ||
  590.     $row[3] != $total_rows-1 ||
  591.     1-$row[4]/(($total_rows-1)/2) > 0.001)
  592.     {
  593.       # PostgreSQL 6.3 fails here
  594.       print "Warning: '$query' returned wrong result: @row\n";
  595.     }
  596.     $sth->finish;
  597.  
  598.     if ($limits->{'func_odbc_mod'})
  599.     {
  600.       $tmp="mod(id,10)";
  601.       if ($limits->{'func_extra_%'})
  602.       {
  603.     $tmp="id % 10";        # For postgreSQL
  604.       }
  605.       $count++;
  606.       if ($limits->{'group_by_alias'}) {
  607.     if (fetch_all_rows($dbh,$query=$server->query("select $tmp as last_digit,count(*) from bench1 group by last_digit")) != 10)
  608.     {
  609.       print "Warning: '$query' returned wrong number of rows\n";
  610.     }
  611.       } elsif ($limits->{'group_by_position'}) {
  612.     if (fetch_all_rows($dbh,$query=$server->query("select $tmp,count(*) from bench1 group by 1")) != 10)
  613.     {
  614.       print "Warning: '$query' returned wrong number of rows\n";
  615.     }
  616.       }
  617.     }
  618.  
  619.     if ($limits->{'order_by_position'} && $limits->{'group_by_position'})
  620.     {
  621.       $count++;
  622.       if (fetch_all_rows($dbh, $query="select id,id3,dummy1 from bench1 where id < 100+$count-$count group by id,id3,dummy1 order by id desc,id3,dummy1") != 100)
  623.       {
  624.     print "Warning: '$query' returned wrong number of rows\n";
  625.       }
  626.     }
  627.     $end_time=new Benchmark;
  628.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  629.                        $small_loop_count));
  630.   }
  631.   print_time($estimated);
  632.   print " for select_group ($count): " .
  633.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  634.  
  635.   $loop_time=new Benchmark;
  636.   $count=$estimated=0;
  637.   for ($tests=1 ; $tests <= $range_loop_count*5 ; $tests++)
  638.   {
  639.     $count+=6;
  640.     fetch_all_rows($dbh,"select min(id) from bench1");
  641.     fetch_all_rows($dbh,"select max(id) from bench1");
  642.     fetch_all_rows($dbh,"select min(id2) from bench1 where id=$tests");
  643.     fetch_all_rows($dbh,"select max(id2) from bench1 where id=$tests");
  644.     if ($limits->{'group_func_sql_min_str'})
  645.     {
  646.       fetch_all_rows($dbh,"select min(dummy1) from bench1 where id=$tests");
  647.       fetch_all_rows($dbh,"select max(dummy1) from bench1 where id=$tests");
  648.     }
  649.     $end_time=new Benchmark;
  650.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  651.                        $range_loop_count*5));
  652.   }
  653.   if ($estimated)
  654.   { print "Estimated time"; }
  655.   else
  656.   { print "Time"; }
  657.   print " for min_max_on_key ($count): " .
  658.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  659.  
  660.   $loop_time=new Benchmark;
  661.   $count=$estimated=0;
  662.   for ($tests=1 ; $tests <= $small_loop_count ; $tests++)
  663.   {
  664.     $count+=6;
  665.     fetch_all_rows($dbh,"select min(id2) from bench1");
  666.     fetch_all_rows($dbh,"select max(id2) from bench1");
  667.     fetch_all_rows($dbh,"select min(id3) from bench1 where id2=$tests");
  668.     fetch_all_rows($dbh,"select max(id3) from bench1 where id2=$tests");
  669.     if ($limits->{'group_func_sql_min_str'})
  670.     {
  671.       fetch_all_rows($dbh,"select min(dummy1) from bench1 where id2=$tests");
  672.       fetch_all_rows($dbh,"select max(dummy1) from bench1 where id2=$tests");
  673.     }
  674.     $end_time=new Benchmark;
  675.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  676.                        $range_loop_count));
  677.   }
  678.   if ($estimated)
  679.   { print "Estimated time"; }
  680.   else
  681.   { print "Time"; }
  682.   print " for min_max ($count): " .
  683.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  684.  
  685.   $loop_time=new Benchmark;
  686.   $count=0;
  687.   $total=$opt_loop_count*3;
  688.   for ($tests=0 ; $tests < $total ; $tests+=$total/100)
  689.   {
  690.     $count+=1;
  691.     fetch_all_rows($dbh,"select count(id) from bench1 where id < $tests");
  692.   }
  693.   $end_time=new Benchmark;
  694.   print "Time for count_on_key ($count): " .
  695.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  696.  
  697.   $loop_time=new Benchmark;
  698.   $count=0;
  699.   for ($tests=0 ; $tests < $total ; $tests+=$total/100)
  700.   {
  701.     $count+=1;
  702.     fetch_all_rows($dbh,"select count(dummy1) from bench1 where id2 < $tests");
  703.   }
  704.   $end_time=new Benchmark;
  705.   print "Time for count ($count): " .
  706.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  707.  
  708.   if ($limits->{'group_distinct_functions'})
  709.   {
  710.     $loop_time=new Benchmark;
  711.     $count=$estimated=0;
  712.     for ($tests=1 ; $tests <= $small_loop_count ; $tests++)
  713.     {
  714.       $count+=2;
  715.       fetch_all_rows($dbh,"select count(distinct dummy1) from bench1");
  716.       fetch_all_rows($dbh,"select dummy1,count(distinct id) from bench1 group by dummy1");
  717.       $end_time=new Benchmark;
  718.       last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  719.                          $small_loop_count));
  720.     }
  721.     if ($estimated)
  722.     { print "Estimated time"; }
  723.     else
  724.     { print "Time"; }
  725.     print " for count_distinct_big ($count): " .
  726.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  727.   }
  728. }
  729.  
  730.  
  731. if ($server->small_rollback_segment())
  732. {
  733.   $dbh->disconnect;                # close connection
  734.   $dbh = $server->connect();
  735. }
  736.  
  737. ####
  738. #### Some updates on the table
  739. ####
  740.  
  741. $loop_time=new Benchmark;
  742.  
  743. if ($limits->{'functions'})
  744. {
  745.   print "\nTesting update of keys with functions\n";
  746.   my $update_loop_count=$opt_loop_count/2;
  747.   for ($i=0 ; $i < $update_loop_count ; $i++)
  748.   {
  749.     my $tmp=$opt_loop_count+$random[$i]; # $opt_loop_count*2 <= $tmp < $total_rows
  750.     $sth = $dbh->do("update bench1 set id3=-$tmp where id3=$tmp") or die $DBI::errstr;
  751.   }
  752.  
  753.   $end_time=new Benchmark;
  754.   print "Time for update_of_key ($update_loop_count):  " .
  755.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  756.  
  757.   if ($opt_lock_tables)
  758.   {
  759.     do_query($dbh,"UNLOCK TABLES");
  760.   }
  761.   if ($opt_fast && defined($server->{vacuum}))
  762.   {
  763.     $server->vacuum(1,\$dbh,"bench1");
  764.   }
  765.   if ($opt_lock_tables)
  766.   {
  767.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  768.   }
  769.  
  770.   if ($server->small_rollback_segment())
  771.   {
  772.     $dbh->disconnect;                # close connection
  773.     $dbh = $server->connect();
  774.   }
  775.  
  776.   $loop_time=new Benchmark;
  777.   $count=0;
  778.   $step=int($opt_loop_count/$range_loop_count+1);
  779.   for ($i= 0 ; $i < $opt_loop_count ; $i+= $step)
  780.   {
  781.     $count++;
  782.     $sth=$dbh->do("update bench1 set id3= 0-id3 where id3 >= 0 and id3 <= $i") or die $DBI::errstr;
  783.   }
  784.  
  785.   if ($server->small_rollback_segment())
  786.   {
  787.     $dbh->disconnect;                # close connection
  788.     $dbh = $server->connect();
  789.   }
  790.   $count++;
  791.   $sth=$dbh->do("update bench1 set id3= 0-id3 where id3 >= 0 and id3 < $opt_loop_count") or die $DBI::errstr;
  792.  
  793.   if ($server->small_rollback_segment())
  794.   {
  795.     $dbh->disconnect;                # close connection
  796.     $dbh = $server->connect();
  797.   }
  798.   $count++;
  799.   $sth=$dbh->do("update bench1 set id3= 0-id3 where id3 >= $opt_loop_count and id3 < ". ($opt_loop_count*2)) or die $DBI::errstr;
  800.  
  801.   #
  802.   # Check that everything was updated
  803.   # In principle we shouldn't time this in the update loop..
  804.   #
  805.  
  806.   if ($server->small_rollback_segment())
  807.   {
  808.     $dbh->disconnect;                # close connection
  809.     $dbh = $server->connect();
  810.   }
  811.   $row_count=0;
  812.   if (($sth=$dbh->prepare("select count(*) from bench1 where id3>=0"))
  813.       && $sth->execute)
  814.   {
  815.     ($row_count)=$sth->fetchrow;
  816.   }
  817.   $result=1 + $opt_loop_count-$update_loop_count;
  818.   if ($row_count != $result)
  819.   {
  820.     print "Warning: Update check returned $row_count instead of $result\n";
  821.   }
  822.  
  823.   $sth->finish;
  824.   if ($server->small_rollback_segment())
  825.   {
  826.     $dbh->disconnect;                # close connection
  827.     $dbh = $server->connect();
  828.   }
  829.   #restore id3 to 0 <= id3 < $total_rows/10 or 0<= id3 < $total_rows
  830.  
  831.   my $func=($limits->{'func_odbc_floor'}) ? "floor((0-id3)/20)" : "0-id3";
  832.   $count++;
  833.   $sth=$dbh->do($query="update bench1 set id3=$func where id3<0") or die $DBI::errstr;
  834.  
  835.   $end_time=new Benchmark;
  836.   print "Time for update_of_key_big ($count): " .
  837.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  838. }
  839. else
  840. {
  841.   print "\nTesting update of keys in loops\n";
  842.   #
  843.   # This is for mSQL that doesn't have functions. Do we really need this ????
  844.   #
  845.  
  846.   $sth=$dbh->prepare("select id3 from bench1 where id3 >= 0") or die $DBI::errstr;
  847.   $sth->execute or die $DBI::errstr;
  848.   $count=0;
  849.   while (@tmp = $sth->fetchrow_array)
  850.   {
  851.     my $tmp1 = "-$tmp[0]";
  852.     my $sth1 = $dbh->do("update bench1 set id3 = $tmp1 where id3 = $tmp[0]");
  853.     $count++;
  854.     $end_time=new Benchmark;
  855.     if (($end_time->[0] - $loop_time->[0]) > $opt_time_limit)
  856.     {
  857.       print "note: Aborting update loop because of timeout\n";
  858.       last;
  859.     }
  860.   }
  861.   $sth->finish;
  862.   # Check that everything except id3=0 was updated
  863.   # In principle we shouldn't time this in the update loop..
  864.   #
  865.   if (fetch_all_rows($dbh,$query="select * from bench1 where id3>=0") != 1)
  866.   {
  867.     if ($count == $total_rows)
  868.     {
  869.       print "Warning: Wrong information after update: Found '$row_count' rows, but should have been: 1\n";
  870.     }
  871.   }
  872.   #restore id3 to 0 <= id3 < $total_rows
  873.   $sth=$dbh->prepare("select id3 from bench1 where id3 < 0") or die $DBI::errstr;
  874.   $sth->execute or die $DBI::errstr;
  875.   while (@tmp = $sth->fetchrow_array)
  876.   {
  877.     $count++;
  878.     my $tmp1 = floor((0-$tmp[0])/10);
  879.     my $sth1 = $dbh->do("update bench1 set id3 = $tmp1 where id3 = $tmp[0]");
  880.   }
  881.   $sth->finish;
  882.   $end_time=new Benchmark;
  883.   $estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  884.                 $opt_loop_count*6);
  885.   if ($estimated)
  886.   { print "Estimated time"; }
  887.   else
  888.   { print "Time"; }
  889.   print " for update_of_key ($count): " .
  890.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  891. }
  892.  
  893. if ($opt_fast && defined($server->{vacuum}))
  894. {
  895.   if ($opt_lock_tables)
  896.   {
  897.     do_query($dbh,"UNLOCK TABLES");
  898.   }
  899.   $server->vacuum(1,\$dbh,"bench1");
  900.   if ($opt_lock_tables)
  901.   {
  902.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  903.   }
  904. }
  905.  
  906. #
  907. # Testing some simple updates
  908. #
  909.  
  910. print "Testing update with key\n";
  911. $loop_time=new Benchmark;
  912. for ($i=0 ; $i < $opt_loop_count*3 ; $i++)
  913. {
  914.   $sth = $dbh->do("update bench1 set dummy1='updated' where id=$i and id2=$i") or die $DBI::errstr;
  915. }
  916.  
  917. $end_time=new Benchmark;
  918. print "Time for update_with_key (" . ($opt_loop_count*3) . "):  " .
  919.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  920.  
  921. $loop_time=new Benchmark;
  922. for ($i=0 ; $i < $opt_loop_count*3 ; $i+=3)
  923. {
  924.   $sth = $dbh->do("update bench1 set dummy1='updated' where id=$i") or die $DBI::errstr;
  925. }
  926.  
  927. $end_time=new Benchmark;
  928. print "Time for update_with_key_prefix (" . ($opt_loop_count) . "):  " .
  929.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  930.  
  931. print "\nTesting update of all rows\n";
  932. $loop_time=new Benchmark;
  933. for ($i=0 ; $i < $small_loop_count ; $i++)
  934. {
  935.   $sth = $dbh->do("update bench1 set dummy1='updated $i'") or die $DBI::errstr;
  936. }
  937. $end_time=new Benchmark;
  938. print "Time for update_big ($small_loop_count):  " .
  939.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  940.  
  941.  
  942. #
  943. # Testing left outer join
  944. #
  945.  
  946. if ($limits->{'func_odbc_floor'} && $limits->{'left_outer_join'})
  947. {
  948.   if ($opt_lock_tables)
  949.   {
  950.     $sth = $dbh->do("LOCK TABLES bench1 a READ, bench1 b READ") || die $DBI::errstr;
  951.   }
  952.   print "\nTesting left outer join\n";
  953.   $loop_time=new Benchmark;
  954.   $count=0;
  955.   for ($i=0 ; $i < $small_loop_count ; $i++)
  956.   {
  957.     $count+=fetch_all_rows($dbh,"select count(*) from bench1 as a left outer join bench1 as b on (a.id2=b.id3)");
  958.   }
  959.   $end_time=new Benchmark;
  960.   print "Time for outer_join_on_key ($small_loop_count:$count):  " .
  961.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  962.  
  963.   $loop_time=new Benchmark;
  964.   $count=0;
  965.   for ($i=0 ; $i < $small_loop_count ; $i++)
  966.   {
  967.     $count+=fetch_all_rows($dbh,"select count(a.dummy1),count(b.dummy1) from bench1 as a left outer join bench1 as b on (a.id2=b.id3)");
  968.   }
  969.   $end_time=new Benchmark;
  970.   print "Time for outer_join ($small_loop_count:$count):  " .
  971.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  972.  
  973.   $count=0;
  974.   $loop_time=new Benchmark;
  975.   for ($i=0 ; $i < $small_loop_count ; $i++)
  976.   {
  977.     $count+=fetch_all_rows($dbh,"select count(a.dummy1),count(b.dummy1) from bench1 as a left outer join bench1 as b on (a.id2=b.id3) where b.id3 is not null");
  978.   }
  979.   $end_time=new Benchmark;
  980.   print "Time for outer_join_found ($small_loop_count:$count):  " .
  981.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  982.  
  983.   $count=$estimated=0;
  984.   $loop_time=new Benchmark;
  985.   for ($i=1 ; $i <= $small_loop_count ; $i++)
  986.   {
  987.     $count+=fetch_all_rows($dbh,"select count(a.dummy1),count(b.dummy1) from bench1 as a left outer join bench1 as b on (a.id2=b.id3) where b.id3 is null");
  988.     $end_time=new Benchmark;
  989.     last if ($estimated=predict_query_time($loop_time,$end_time,
  990.                        \$count,$i,
  991.                        $range_loop_count));
  992.   }
  993.   if ($estimated)
  994.   { print "Estimated time"; }
  995.   else
  996.   { print "Time"; }
  997.   print " for outer_join_not_found ($range_loop_count:$count):  " .
  998.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  999.  
  1000.   if ($opt_lock_tables)
  1001.   {
  1002.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1003.   }
  1004. }
  1005.  
  1006. if ($server->small_rollback_segment())
  1007. {
  1008.   $dbh->disconnect;                # close connection
  1009.   $dbh = $server->connect();
  1010. }
  1011.  
  1012. ####
  1013. #### Test INSERT INTO ... SELECT
  1014. ####
  1015.  
  1016. if ($limits->{'insert_select'})
  1017. {
  1018.   if ($opt_lock_tables)
  1019.   {
  1020.     $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  1021.   }
  1022.   print "\nTesting INSERT INTO ... SELECT\n";
  1023.   do_many($dbh,$server->create("bench2",
  1024.                    ["id int NOT NULL",
  1025.                 "id2 int NOT NULL",
  1026.                 "id3 int NOT NULL",
  1027.                 "dummy1 char(30)"],
  1028.                    ["primary key (id,id2)"]));
  1029.   do_many($dbh,$server->create("bench3",
  1030.                    ["id int NOT NULL",
  1031.                 "id2 int NOT NULL",
  1032.                 "id3 int NOT NULL",
  1033.                 "dummy1 char(30)"],
  1034.                    ["primary key (id,id2)",
  1035.                 "index index_id3 (id3)"]));
  1036.   $loop_time=new Benchmark;
  1037.   $sth = $dbh->do("INSERT INTO bench2 SELECT * from bench1") ||
  1038.     die $DBI::errstr;
  1039.   $end_time=new Benchmark;
  1040.   print "Time for insert_select_1_key (1):  " .
  1041.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1042.   $loop_time=new Benchmark;
  1043.   $sth = $dbh->do("INSERT INTO bench3 SELECT * from bench1") ||
  1044.     die $DBI::errstr;
  1045.   $end_time=new Benchmark;
  1046.   print "Time for insert_select_2_keys (1):  " .
  1047.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1048.   $loop_time=new Benchmark;
  1049.   $sth = $dbh->do("DROP TABLE bench2" . $server->{'drop_attr'}) ||
  1050.     die $DBI::errstr;
  1051.   $sth = $dbh->do("DROP TABLE bench3" . $server->{'drop_attr'}) ||
  1052.     die $DBI::errstr;
  1053.   $end_time=new Benchmark;
  1054.   print "Time for drop table(2): " .
  1055.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1056.  
  1057.   if ($opt_fast && defined($server->{vacuum}))
  1058.   {
  1059.     $server->vacuum(1,\$dbh);
  1060.   }
  1061.   if ($server->small_rollback_segment())
  1062.   {
  1063.     $dbh->disconnect;                # close connection
  1064.     $dbh = $server->connect();
  1065.   }
  1066.   if ($opt_lock_tables)
  1067.   {
  1068.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1069.   }
  1070. }
  1071.  
  1072.  
  1073. ####
  1074. #### Do some deletes on the table
  1075. ####
  1076.  
  1077. if (!$opt_skip_delete)
  1078. {
  1079.   print "\nTesting delete\n";
  1080.   $loop_time=new Benchmark;
  1081.   $count=0;
  1082.   for ($i=0 ; $i < $opt_loop_count ; $i+=10)
  1083.   {
  1084.     $count++;
  1085.     $tmp=$opt_loop_count+$random[$i]; # $opt_loop_count*2 <= $tmp < $total_rows
  1086.     $dbh->do("delete from bench1 where id3=$tmp") or die $DBI::errstr;
  1087.   }
  1088.  
  1089.   $end_time=new Benchmark;
  1090.   print "Time for delete_key ($count): " .
  1091.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1092.  
  1093.   if ($server->small_rollback_segment())
  1094.   {
  1095.     $dbh->disconnect;                # close connection
  1096.     $dbh = $server->connect();
  1097.   }
  1098.  
  1099.   $count=0;
  1100.   $loop_time=new Benchmark;
  1101.   for ($i= 0 ; $i < $opt_loop_count ; $i+=$opt_loop_count/10)
  1102.   {
  1103.     $sth=$dbh->do("delete from bench1 where id3 >= 0 and id3 <= $i") or die $DBI::errstr;
  1104.     $count++;
  1105.   }
  1106.   $count+=2;
  1107.   if ($server->small_rollback_segment())
  1108.   {
  1109.     $dbh->disconnect;                # close connection
  1110.     $dbh = $server->connect();
  1111.   }
  1112.   $sth=$dbh->do("delete from bench1 where id3 >= 0 and id3 <= $opt_loop_count") or die $DBI::errstr;
  1113.   if ($server->small_rollback_segment())
  1114.   {
  1115.     $dbh->disconnect;                # close connection
  1116.     $dbh = $server->connect();
  1117.   }
  1118.  
  1119.   $sth=$dbh->do("delete from bench1 where id >= $opt_loop_count and id <= " . ($opt_loop_count*2) ) or die $DBI::errstr;
  1120.  
  1121.   if ($server->small_rollback_segment())
  1122.   {
  1123.     $dbh->disconnect;                # close connection
  1124.     $dbh = $server->connect();
  1125.   }
  1126.   if ($opt_fast)
  1127.   {
  1128.     $sth=$dbh->do("delete from bench1") or die $DBI::errstr;
  1129.   }
  1130.   else
  1131.   {
  1132.     $sth = $dbh->do("delete from bench1 where id3 < " . ($total_rows)) or die $DBI::errstr;
  1133.   }
  1134.  
  1135.   $end_time=new Benchmark;
  1136.   print "Time for delete_all ($count): " .
  1137.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1138.  
  1139.   if ($opt_lock_tables)
  1140.   {
  1141.     $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  1142.   }
  1143.   $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  1144. }
  1145.  
  1146. if ($server->small_rollback_segment())
  1147. {
  1148.   $dbh->disconnect;                # close connection
  1149.   $dbh = $server->connect();
  1150. }
  1151. if ($opt_fast && defined($server->{vacuum}))
  1152. {
  1153.   $server->vacuum(1,\$dbh);
  1154. }
  1155.  
  1156.  
  1157. keys_test:
  1158. #
  1159. # Test of insert in table with many keys
  1160. # This test assumes that the server really create the keys!
  1161. #
  1162.  
  1163. my @fields=(); my @keys=();
  1164. $keys=min($limits->{'max_index'},16);          # 16 is more than enough
  1165. $seg= min($limits->{'max_index_parts'},$keys,16); # 16 is more than enough
  1166.  
  1167. print "Insert into table with $keys keys and with a primary key with $seg parts\n";
  1168.  
  1169. # Make keys on the most important types
  1170. @types=(0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1);    # A 1 for each char field
  1171. push(@fields,"field1 tinyint not null");
  1172. push(@fields,"field2 mediumint not null");
  1173. push(@fields,"field3 smallint not null");
  1174. push(@fields,"field4 char(16) not null");
  1175. push(@fields,"field5 integer not null");
  1176. push(@fields,"field6 float not null");
  1177. push(@fields,"field7 double not null");
  1178. for ($i=8 ; $i <= $keys ; $i++)
  1179. {
  1180.   push(@fields,"field$i char(6) not null");    # Should be relatively fair
  1181. }
  1182.  
  1183. # First key contains many segments
  1184. $query="primary key (";
  1185. for ($i= 1 ; $i <= $seg ; $i++)
  1186. {
  1187.   $query.= "field$i,";
  1188. }
  1189. substr($query,-1)=")";
  1190. push (@keys,$query);
  1191.  
  1192. #Create other keys
  1193. for ($i=2 ; $i <= $keys ; $i++)
  1194. {
  1195.   push(@keys,"index index$i (field$i)");
  1196. }
  1197.  
  1198. do_many($dbh,$server->create("bench1",\@fields,\@keys));
  1199. if ($opt_lock_tables)
  1200. {
  1201.   $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1202. }
  1203.  
  1204. if ($server->small_rollback_segment())
  1205. {
  1206.   $dbh->disconnect;                # close connection
  1207.   $dbh = $server->connect();
  1208. }
  1209.  
  1210. $loop_time=new Benchmark;
  1211. $fields=$#fields;
  1212. if (($opt_fast || $opt_fast_insert) && $limits->{'insert_multi_value'})
  1213. {
  1214.   $query_size=$server->{'limits'}->{'query_size'};
  1215.   $query="insert into bench1 values ";
  1216.   $res=$query;
  1217.   for ($i=0; $i < $many_keys_loop_count; $i++)
  1218.   {
  1219.     $rand=$random[$i];
  1220.     $tmp="(" . ($i & 127) . ",$rand," . ($i & 32766) .
  1221.       ",'ABCDEF$rand',0,";
  1222.  
  1223.     for ($j=5; $j <= $fields ; $j++)
  1224.     {
  1225.       $tmp.= ($types[$j] == 0) ? "$rand," : "'$rand',";
  1226.     }
  1227.     substr($tmp,-1)=")";
  1228.     if (length($tmp)+length($res) < $query_size)
  1229.     {
  1230.       $res.= $tmp . ",";
  1231.     }
  1232.     else
  1233.     {
  1234.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  1235.       $res=$query . $tmp . ",";
  1236.     }
  1237.   }
  1238.   $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  1239. }
  1240. else
  1241. {
  1242.   for ($i=0; $i < $many_keys_loop_count; $i++)
  1243.   {
  1244.     $rand=$random[$i];
  1245.     $query="insert into bench1 values (" . ($i & 127) . ",$rand," . ($i & 32767) .
  1246.       ",'ABCDEF$rand',0,";
  1247.  
  1248.     for ($j=5; $j <= $fields ; $j++)
  1249.     {
  1250.       $query.= ($types[$j] == 0) ? "$rand," : "'$rand',";
  1251.     }
  1252.     substr($query,-1)=")";
  1253.     print "query1: $query\n" if ($opt_debug);
  1254.     $dbh->do($query) or die "Got error $DBI::errstr with query: $query\n";
  1255.   }
  1256. }
  1257. $end_time=new Benchmark;
  1258. print "Time for insert_key ($many_keys_loop_count): " .
  1259.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1260.  
  1261. if ($server->small_rollback_segment())
  1262. {
  1263.   $dbh->disconnect;                # close connection
  1264.   $dbh = $server->connect();
  1265. }
  1266. if ($opt_fast && defined($server->{vacuum}))
  1267. {
  1268.   if ($opt_lock_tables)
  1269.   {
  1270.     do_query($dbh,"UNLOCK TABLES");
  1271.   }
  1272.   $server->vacuum(1,\$dbh,"bench1");
  1273.   if ($opt_lock_tables)
  1274.   {
  1275.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1276.   }
  1277. }
  1278.  
  1279. #
  1280. # update one key of the above
  1281. #
  1282.  
  1283. print "Testing update of keys\n";
  1284. $loop_time=new Benchmark;
  1285. for ($i=0 ; $i< 256; $i++)
  1286. {
  1287.   $dbh->do("update bench1 set field5=1 where field1=$i")
  1288.     or die "Got error $DBI::errstr with query: update bench1 set field5=1 where field1=$i\n";
  1289. }
  1290. $end_time=new Benchmark;
  1291. print "Time for update_of_primary_key_many_keys (256): " .
  1292.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1293.  
  1294. if ($server->small_rollback_segment())
  1295. {
  1296.   $dbh->disconnect;                # close connection
  1297.   $dbh = $server->connect();
  1298. }
  1299. if ($opt_fast && defined($server->{vacuum}))
  1300. {
  1301.   if ($opt_lock_tables)
  1302.   {
  1303.     do_query($dbh,"UNLOCK TABLES");
  1304.   }
  1305.   $server->vacuum(1,\$dbh,"bench1");
  1306.   if ($opt_lock_tables)
  1307.   {
  1308.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1309.   }
  1310. }
  1311.  
  1312. if ($server->small_rollback_segment())
  1313. {
  1314.   $dbh->disconnect;                # close connection
  1315.   $dbh = $server->connect();
  1316. }
  1317.  
  1318. #
  1319. # Delete everything from table
  1320. #
  1321.  
  1322. print "Deleting rows from the table\n";
  1323. $loop_time=new Benchmark;
  1324. $count=0;
  1325.  
  1326. for ($i=0 ; $i < 128 ; $i++)
  1327. {
  1328.   $count++;
  1329.   $dbh->do("delete from bench1 where field1 = $i") or die $DBI::errstr;
  1330. }
  1331.  
  1332. $end_time=new Benchmark;
  1333. print "Time for delete_big_many_keys ($count): " .
  1334. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1335.  
  1336. print "Deleting everything from table\n";
  1337. $count=1;
  1338. if ($opt_fast)
  1339. {
  1340.   $dbh->do("delete from bench1") or die $DBI::errstr;
  1341. }
  1342. else
  1343. {
  1344.   $dbh->do("delete from bench1 where field1 > 0") or die $DBI::errstr;
  1345. }
  1346.  
  1347. if ($opt_lock_tables)
  1348. {
  1349.   $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  1350. }
  1351.  
  1352. $end_time=new Benchmark;
  1353. print "Time for delete_all_many_keys ($count): " .
  1354.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1355.  
  1356. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  1357. if ($opt_fast && defined($server->{vacuum}))
  1358. {
  1359.   $server->vacuum(1,\$dbh);
  1360. }
  1361.  
  1362. #
  1363. # Test multi value inserts if the server supports it
  1364. #
  1365.  
  1366. if ($limits->{'insert_multi_value'})
  1367. {
  1368.   $query_size=$limits->{'query_size'}; # Same limit for all databases
  1369.  
  1370.   $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'});
  1371.   do_many($dbh,$server->create("bench1",
  1372.                    ["id int NOT NULL",
  1373.                 "id2 int NOT NULL",
  1374.                 "id3 int NOT NULL",
  1375.                 "dummy1 char(30)"],
  1376.                    ["primary key (id,id2)",
  1377.                    "index index_id3 (id3)"]));
  1378.   if ($opt_lock_tables)
  1379.   {
  1380.     $sth = $dbh->do("LOCK TABLES bench1 write") || die $DBI::errstr;
  1381.   }
  1382.  
  1383.   $loop_time=new Benchmark;
  1384.   print "Inserting $opt_loop_count rows with multiple values\n";
  1385.   $query="insert into bench1 values ";
  1386.   $res=$query;
  1387.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  1388.   {
  1389.     my $tmp= "($i,$i,$i,'EFGHIJKLM'),";
  1390.     if (length($i)+length($res) < $query_size)
  1391.     {
  1392.       $res.= $tmp;
  1393.     }
  1394.     else
  1395.     {
  1396.       do_query($dbh,substr($res,0,length($res)-1));
  1397.       $res=$query .$tmp;
  1398.     }
  1399.   }
  1400.   do_query($dbh,substr($res,0,length($res)-1));
  1401.  
  1402.   if ($opt_lock_tables)
  1403.   {
  1404.     $sth = $dbh->do("UNLOCK TABLES ") || die $DBI::errstr;
  1405.   }
  1406.  
  1407.   $end_time=new Benchmark;
  1408.   print "Time for multiple_value_insert (" . ($opt_loop_count) . "): " .
  1409.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1410.  
  1411.   if ($opt_fast && defined($server->{vacuum}))
  1412.   {
  1413.     $server->vacuum(1,\$dbh);
  1414.   }
  1415.   if ($opt_lock_tables)
  1416.   {
  1417.     $sth = $dbh->do("UNLOCK TABLES ") || die $DBI::errstr;
  1418.   }
  1419.  
  1420.   # A big table may take a while to drop
  1421.   $loop_time=new Benchmark;
  1422.   $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  1423.   $end_time=new Benchmark;
  1424.   print "Time for drop table(1): " .
  1425.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1426. }
  1427.  
  1428. ####
  1429. #### End of benchmark
  1430. ####
  1431.  
  1432. $dbh->disconnect;                # close connection
  1433.  
  1434. end_benchmark($start_time);
  1435.  
  1436. ###
  1437. ### Some help functions
  1438. ###
  1439.  
  1440.  
  1441. # Do some sample selects on direct key
  1442. # First select finds a row, the second one doesn't find.
  1443.  
  1444. sub check_select_key
  1445. {
  1446.   my ($sel_columns,$column,$check)= @_;
  1447.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$count,$row_count,$estimated);
  1448.  
  1449.   $estimated=0;
  1450.   $loop_time=new Benchmark;
  1451.   $count=0;
  1452.   for ($i=1 ; $i <= $opt_read_key_loop_count; $i++)
  1453.   {
  1454.     $count+=2;
  1455.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  1456.     $tmp=$tmpvar % ($total_rows);
  1457.     fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp")
  1458.       or die $DBI::errstr;
  1459.     $tmp+=$total_rows;
  1460.     defined($row_count=fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp")) or die $DBI::errstr;
  1461.     die "Found $row_count rows on impossible id: $tmp\n" if ($row_count);
  1462.     $end_time=new Benchmark;
  1463.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i,
  1464.                        $opt_loop_count));
  1465.   }
  1466.   if ($estimated)
  1467.   { print "Estimated time"; }
  1468.   else
  1469.   { print "Time"; }
  1470.   print " for $check ($count): " .
  1471.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1472. }
  1473.  
  1474. # Same as above, but select on 2 columns
  1475.  
  1476. sub check_select_key2
  1477. {
  1478.   my ($sel_columns,$column,$column2,$check)= @_;
  1479.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$count,$row_count,$estimated);
  1480.  
  1481.   $estimated=0;
  1482.   $loop_time=new Benchmark;
  1483.   $count=0;
  1484.   for ($i=1 ; $i <= $opt_read_key_loop_count; $i++)
  1485.   {
  1486.     $count+=2;
  1487.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  1488.     $tmp=$tmpvar % ($total_rows);
  1489.     fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp and $column2=$tmp")
  1490.       or die $DBI::errstr;
  1491.     $tmp+=$total_rows;
  1492.     defined($row_count=fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp and $column2=$tmp")) or die $DBI::errstr;
  1493.     die "Found $row_count rows on impossible id: $tmp\n" if ($row_count);
  1494.     $end_time=new Benchmark;
  1495.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i,
  1496.                        $opt_loop_count));
  1497.   }
  1498.   if ($estimated)
  1499.   { print "Estimated time"; }
  1500.   else
  1501.   { print "Time"; }
  1502.   print " for $check ($count): " .
  1503.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1504. }
  1505.  
  1506. #
  1507. # Search using some very simple queries
  1508. #
  1509.  
  1510. sub check_select_range
  1511. {
  1512.   my ($column,$check)= @_;
  1513.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$query,$rows,$estimated);
  1514.  
  1515.   $estimated=0;
  1516.   $loop_time=new Benchmark;
  1517.   $found=$count=0;
  1518.   for ($test=1 ; $test <= $range_loop_count; $test++)
  1519.   {
  1520.     $count+=$#Q+1;
  1521.     for ($i=0 ; $i < $#Q ; $i+=2)
  1522.     {
  1523.       $query=$Q[$i];
  1524.       $rows=$Q[$i+1];
  1525.       $query =~ s/!id!/$column/g;
  1526.       if (($row_count=fetch_all_rows($dbh,$query)) != $rows)
  1527.       {
  1528.     if ($row_count == undef())
  1529.     {
  1530.       die "Got error: $DBI::errstr when executing $query\n";
  1531.     }
  1532.     die "'$query' returned wrong number of rows: $row_count instead of $rows\n";
  1533.       }
  1534.       $found+=$row_count;
  1535.     }
  1536.     $end_time=new Benchmark;
  1537.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$test,
  1538.                        $range_loop_count));
  1539.   }
  1540.   if ($estimated)
  1541.   { print "Estimated time"; }
  1542.   else
  1543.   { print "Time"; }
  1544.   print " for $check ($count:$found): " .
  1545.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1546. }
  1547.  
  1548.  
  1549. #
  1550. # SELECT * from bench where col=x or col=x or col=x ...
  1551.  
  1552.  
  1553. sub check_or_range
  1554. {
  1555.   my ($column,$check)= @_;
  1556.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$columns,$estimated,$found,
  1557.       $or_part,$count,$loop_count);
  1558.  
  1559.   $columns=min($limits->{'max_columns'},50,($limits->{'query_size'}-50)/13);
  1560.   $columns=$columns- ($columns % 4); # Make Divisible by 4
  1561.  
  1562.   $estimated=0;
  1563.   $loop_time=new Benchmark;
  1564.   $found=0;
  1565.   # The number of tests must be divisible by the following
  1566.   $tmp= $limits->{'func_extra_in_num'} ? 15 : 10; 
  1567.   # We need to calculate the exact number of test to make 'Estimated' right
  1568.   $loop_count=$range_loop_count*10+$tmp-1;
  1569.   $loop_count=$loop_count- ($loop_count % $tmp);
  1570.   
  1571.   for ($count=0 ; $count < $loop_count ; )
  1572.   {
  1573.     for ($rowcnt=0; $rowcnt <= $columns; $rowcnt+= $columns/4)
  1574.     {
  1575.       my $query="select * from bench1 where ";
  1576.       my $or_part= "$column = 1";
  1577.       $count+=2;
  1578.  
  1579.       for ($i=1 ; $i < $rowcnt ; $i++)
  1580.       {
  1581.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  1582.     $tmp=$tmpvar % ($opt_loop_count*4);
  1583.     $or_part.=" or $column=$tmp";
  1584.       }
  1585.       print $query . $or_part . "\n" if ($opt_debug);
  1586.       ($rows=fetch_all_rows($dbh,$query . $or_part)) or die $DBI::errstr;
  1587.       $found+=$rows;
  1588.  
  1589.       if ($limits->{'func_extra_in_num'})
  1590.       {
  1591.     my $in_part=$or_part;    # Same query, but use 'func_extra_in_num' instead.
  1592.     $in_part=~ s/ = / IN \(/;
  1593.     $in_part=~ s/ or $column=/,/g;
  1594.     $in_part.= ")";
  1595.     fetch_all_rows($dbh,$query . $in_part) or die $DBI::errstr;
  1596.     $count++;
  1597.       }
  1598.       # Do it a little harder by setting a extra range
  1599.       defined(($rows=fetch_all_rows($dbh,"$query($or_part) and $column < 10"))) or die $DBI::errstr;
  1600.       $found+=$rows;
  1601.     }
  1602.     $end_time=new Benchmark;
  1603.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  1604.                        $loop_count));
  1605.   }
  1606.  
  1607.   if ($estimated)
  1608.   { print "Estimated time"; }
  1609.   else
  1610.   { print "Time"; }
  1611.   print " for $check ($count:$found): " .
  1612.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1613. }
  1614.